window.onload = function(){
    var href = decodeURI(location.href)
    console.log(href);

function getQueryString(url){
    //通过？分割数组
    let cs = url.split("?")[1]
    console.log(cs);

    let and = cs.split("&")[0]
    console.log(and);
    let title = and.split("=")[1]
    console.log(title);
    var tiao = document.querySelector(".tiao")
    tiao.innerHTML = title
    //判断如果没有参数，返回null
    if(!cs){
        return null;
    }
    
    //针对所有参数分割成数组
    let csarr = cs.split("&")
    console.log(csarr);

    //定义一个接受结果的对象
    let csobj = {};
    //遍历所有的参数数组，通过=将key和value分割存入对象
    csarr.forEach(item => {
        csobj[item.split("=")[0]] = item.split("=")[1];
    });
    console.log(csobj);
    return csobj;
}
var cs = getQueryString(href);
console.log(cs);
var productList = document.querySelector(".productList")
console.log(productList)



fetch("http://chst.vip:1234/api/getproductlist?categoryid="+cs.categoryid)
    .then(data=>{
        return data.json();
    })
    .then(res=>{
        console.log(res);
        console.log(res.result);
        let ele = "";
        res.result.forEach(item=>{
            ele += `<li class="product" productId=${item.productId}>
                        <a href="../pages/productlist.html?productId=${item.productId}">
                        <div class="product_left">
                            ${item.productImg}
                        </div>
                        <div class="product_right">
                            <h4>${item.productName}</h4>
                            <p>${item.productPrice}</p>
                            <div class="product_bottom">
                                <span>${item.productQuote}</span>
                                <i>
                                ${item.productCom}
                                </i>
                            </div> 
                        </div>
                        </a>
                    </li>`
        })
        productList.innerHTML = ele
     
    })
    console.log(productList);
}
